package edu.whu.demo.service;

import edu.whu.demo.entity.CommodityItem;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author chasezhuang
 */
@Service
public class CommodityService {

    // 创建线程安全的Map，模拟Todo信息的存储
    private Map<Long, CommodityItem> commodities =
            Collections.synchronizedMap(new HashMap<Long, CommodityItem>());

    public CommodityItem addCommodity(CommodityItem commodity) {
        commodities.put(commodity.getId(), commodity);
        return commodity;
    }

    public CommodityItem getCommodity(long id) {
        return commodities.get(id);
    }

    public List<CommodityItem> findCommodities(String name, Boolean complete) {
        List<CommodityItem> result=new ArrayList<>();
        for (CommodityItem commodity: commodities.values()){
            if (name!=null && !commodity.getName().contains(name)) {
                continue;
            }
            result.add(commodity);
        }
        return result;
    }

    public void updateCommodity(long id, CommodityItem commodity) {
        CommodityItem commodity2  = commodities.get(id);
        commodity2.setName(commodity.getName());
        commodities.put(id, commodity2);
    }

    public void deleteCommodity(long id) {
        commodities.remove(id);
    }


}
